﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using OGS.Contracts;

namespace OGS.DataAccess
{
    /// <summary>
    /// Represents a class used for database related work of Brand
    /// </summary>
    public class DABrand
    {
        private static readonly DABrand instance;

        /// <summary>
        /// Initializes a static instance of the DABrand class with no arguments.
        /// </summary>
        static DABrand()
        {
            instance = new DABrand();
        }

        /// <summary>
        /// Initializes a new instance of the DABrand class with no arguments.
        /// </summary>
        private DABrand() { }

        /// <summary>
        /// The Instance of DABrand class
        /// </summary>
        public static DABrand Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Saves the Brand
        /// </summary>
        /// <param name="objBrandInfo"> The BrandInfo Object </param>
        /// <returns>Success/Faliure</returns>
        public int SaveBrand(Brand objBrandInfo)
        {
            using (DBManager dbm = new DBManager("stp_Save_Brand"))
            {
                dbm.Add("@BrandID", objBrandInfo.ID, ParameterDirection.InputOutput);
                dbm.Add("@Name", objBrandInfo.Name);
                dbm.Add("@IsActive", objBrandInfo.IsActive);
                if (objBrandInfo.LastModifiedBy > 0)
                {
                    dbm.Add("@LastModifiedBy", objBrandInfo.LastModifiedBy);
                }
                dbm.Add("@LastModifiedDate", objBrandInfo.LastModifiedDate);

                dbm.ExecuteNonQuery();

                objBrandInfo.ID = Convert.ToInt32(dbm.GetValueInInt("@BrandID"));
                return Convert.ToInt32(dbm.ReturnValue);
            }
        }

        /// <summary>
        /// Deletes the Brand
        /// </summary>
        /// <param name="ID"> The BrandInfo ID </param>
        /// <returns>Success/Faliure</returns>
        public int DeleteBrand(int ID)
        {
            using (DBManager dbm = new DBManager("stp_Delete_Brand"))
            {
                dbm.Add("@BrandID", ID);

                dbm.ExecuteNonQuery();
                return Convert.ToInt32(dbm.ReturnValue);
            }
        }

        /// <summary>
        /// Get Brand Details
        /// </summary>
        /// <param name="ID"> The Brand ID </param>
        /// <returns>The Brand DataTable </returns>
        public Brand GetBrandDetails(int ID)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Get_BrandDetails"))
            {
                dbm.Add("@BrandID", ID);

                dt = dbm.GetDataTable();
            }

            Brand objBrandInfo = new Brand();
            if (dt.Rows.Count > 0)
            {
                objBrandInfo = PopulateBrand(dt.Rows[0]);
            }
            return objBrandInfo;
        }

        /// <summary>
        /// Search the Brand
        /// </summary>
        /// <param name="objBrandInfo"> The BrandInfo Object </param>
        /// <param name="objSearch"> The SearchAttributes Object </param>
        /// <returns>The Brand DataTable </returns>
        public List<Brand> SearchBrand(Brand objBrandInfo, SearchAttributes objSearch)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Search_Brand", objSearch))
            {
                dbm.Add("@Name", objBrandInfo.Name);
                //dbm.Add("@IsActive", objBrandInfo.IsActive);
                dt = dbm.GetDataTable();

                objSearch.TotalRecordCount = Convert.ToInt32(dbm.GetValueInInt("@TotalRecordCount"));
            }

            List<Brand> lstBrand = new List<Brand>();
            foreach (DataRow dr in dt.Rows)
            {
                Brand objBrand = PopulateBrand(dr);
                lstBrand.Add(objBrand);
            }
            return lstBrand;

        }

        /// <summary>
        /// Get Brands
        /// </summary>
        public List<Brand> GetBrands()
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Get_Brands"))
            {
                dt = dbm.GetDataTable();
            }

            List<Brand> lstBrand = new List<Brand>();
            foreach (DataRow dr in dt.Rows)
            {
                lstBrand.Add(new Brand
                {
                    ID = Convert.ToInt32(dr["PK_BrandID"]),
                    Name = Convert.ToString(dr["Name"])
                });
            }

            return lstBrand;
        }

        /// <summary>
        /// Populate the Brand
        /// </summary>
        /// <param name="dr"> The BrandInfo DataRow </param>
        /// <returns>The Brand Object </returns>
        public Brand PopulateBrand(DataRow dr)
        {
            Brand objBrandInfo = new Brand();

            objBrandInfo.ID = Convert.ToInt32(dr["PK_BrandID"]);
            objBrandInfo.Name = Convert.ToString(dr["Name"]);
            objBrandInfo.IsActive = Convert.ToBoolean(dr["IsActive"]);
            objBrandInfo.LastModifiedBy = Convert.ToInt32(dr["LastModifiedBy"]);
            objBrandInfo.LastModifiedDate = Convert.ToDateTime(dr["LastModifiedDate"]);

            return objBrandInfo;
        }


    }
}
